{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37c7a949-aa81-4ece-878b-72b97a8a6055",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import zipfile\n",
    "import os\n",
    " \n",
    "# 指定ZIP文件的路径\n",
    "zip_path = '../new_uspto_mol.zip'\n",
    " \n",
    "# 指定解压后的文件存放目录\n",
    "extract_to = '../new_uspto_mol'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4eeae594-16a2-4423-ae00-eb2f45d5da0c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 检查解压目录是否存在，如果不存在则创建\n",
    "if not os.path.exists(extract_to):\n",
    "    os.makedirs(extract_to)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81e13742-ed84-440b-9a3f-dbe52ba1a834",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 打开ZIP文件\n",
    "with zipfile.ZipFile(zip_path, 'r') as zip_ref:\n",
    "    # 解压所有文件到指定目录\n",
    "    \n",
    "    zip_ref.extractall(extract_to)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0458a2b1-bcc9-4c7a-a61f-3fc185c7493c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**************过滤前后长度比较****************\n",
      "file_path: 10000\n",
      "path_exists: 10000\n",
      "过滤后的数据长度:\n",
      "10000\n",
      "数据已保存到exit_train.csv\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "# 读取CSV文件\n",
    "df = pd.read_csv(\"D:\\\\Data\\\\Train\\\\new_uspto_mol\\\\uspto_mol\\\\new_train.csv\")\n",
    "\n",
    "# 假设CSV文件中有一列名为'file_path'\n",
    "file_path_column = 'file_path'\n",
    "base_path = 'D:\\\\Data\\\\Train\\\\new_uspto_mol\\\\'\n",
    "\n",
    "# 去除file_path列中每个路径的空格\n",
    "df[file_path_column] = df[file_path_column].str.strip()\n",
    "\n",
    "# 过滤掉file_path列中为空或仅包含空格的行\n",
    "df = df[df[file_path_column].str.len() > 0]\n",
    "\n",
    "# 检查每个路径是否存在\n",
    "def check_path_exists(relative_path):\n",
    "    full_path = os.path.join(base_path, relative_path)\n",
    "    exists = os.path.exists(full_path)\n",
    "    # print(f\"检查路径: {full_path}, 存在: {exists}\")\n",
    "    return exists\n",
    "\n",
    "df['path_exists'] = df[file_path_column].apply(check_path_exists)\n",
    "\n",
    "print(\"**************过滤前后长度比较****************\")\n",
    "print(\"file_path:\", len(df[\"file_path\"]))\n",
    "print(\"path_exists:\", len(df[\"path_exists\"]))\n",
    "\n",
    "# 过滤掉那些路径不存在的行\n",
    "df_filtered = df[df['path_exists']]\n",
    "\n",
    "# 打印过滤后的DataFrame\n",
    "print(\"过滤后的数据长度:\")\n",
    "print(len(df_filtered[\"file_path\"]))\n",
    "\n",
    "# 删除辅助列'path_exists'并保存\n",
    "if not df_filtered.empty:\n",
    "    train_df = df_filtered.drop('path_exists', axis=1)\n",
    "    train_df.to_csv('D:\\\\Data\\\\Train\\\\new_uspto_mol\\\\uspto_mol\\\\new_train.csv', index=False)\n",
    "    print(\"数据已保存到exit_train.csv\")\n",
    "else:\n",
    "    print(\"没有有效的路径，未保存任何数据。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
